﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StockMatch.Host.Task;
using StockMatch.Service;
using StockMatch.Service.Entity;
using ThreadTask;
using Titan;
namespace StockMatch.Host
{
    public class StockMatch:IStockMatch
    {
        /// <summary>
        /// 操盘中的天天赢方案
        /// </summary>
        private static Dictionary<int, TaskBase> DayWinTasks = new Dictionary<int, TaskBase>();

        public static TaskPool DayWinProjectTaskPool;

        public static void Init()
        {
            DayWinProjectTaskPool = TaskPool.Create("DayWinProjectTaskPool", 4);

            Thread thread=new Thread(new ThreadStart(DayWinProjectManagerThread));
            thread.Start();
        }



        private static  void LoadDayWinTask()
        {
            QueryExpression query = new QueryExpression();
            query.EntityType = typeof (DayWinProject);
            query.Select(DayWinProject_.ProjectId);
            query.Select(DayWinProject_.ManageMoney);
            query.Where(DayWinProject_.ProjectState.TGreaterThanOrEqual(ProjectStateEnum.Trading));//加载正在操盘中的方案
            DayWinProjects dayWinProjects=new DayWinProjects();
            using (IDbSession cn = Environment.OpenSession())
            {
                cn.SelectCollection(dayWinProjects.Items, query);
            }
            foreach (DayWinProject dayWinProject in dayWinProjects.Items)
            {
                DayWinProjectManager  dayWinProjectManager=new DayWinProjectManager();
                dayWinProjectManager.ProjectId = dayWinProject.ProjectId;
                dayWinProjectManager.Key = dayWinProject.ProjectId.ToString();
               
                if (!DayWinTasks.ContainsKey(dayWinProjectManager.ProjectId)) {
                    DayWinTasks.Add(dayWinProjectManager.ProjectId, dayWinProjectManager);
                }
            }
        }

        private static void DayWinProjectManagerThread()
        {
            LoadDayWinTask();
            while (true)
            {
                foreach (DayWinProjectManager task in DayWinTasks.Values)
                {
                    DayWinProjectTaskPool.AddToBegin(task);
                }
                Thread.Sleep(1000*2);
            }
        }

        public void DayWinProjectOpenedAccount(int projectId)
        {
            DayWinProject dayWinProject = new DayWinProject {ProjectId = projectId};
            using (IDbSession cn=Environment.OpenSession())
            {
                if (cn.Select(dayWinProject) && dayWinProject.ProjectState >= (int)ProjectStateEnum.Trading && dayWinProject.ProjectState < (int)ProjectStateEnum.SubmitTermination)
                {
                    DayWinProjectManager dayWinProjectManager = new DayWinProjectManager();
                    dayWinProjectManager.ProjectId = dayWinProject.ProjectId;
                    dayWinProjectManager.Key = dayWinProject.ProjectId.ToString();
                    if (!DayWinTasks.ContainsKey(dayWinProjectManager.ProjectId))
                    {
                        DayWinTasks.Add(dayWinProjectManager.ProjectId, dayWinProjectManager);
                    }
                }
            }
        }

        public void DayWinProjectTermination(int projectId)
        {
            DayWinProject dayWinProject = new DayWinProject { ProjectId = projectId };
            using (IDbSession cn = Environment.OpenSession())
            {
                if (cn.Select(dayWinProject) && dayWinProject.ProjectState>=(int)ProjectStateEnum.SubmitTermination)
                {
                    if (DayWinTasks.ContainsKey(dayWinProject.ProjectId))
                    {
                        DayWinTasks.Remove(dayWinProject.ProjectId);
                    }
                }
            }
        }
    }
}
